package com.ytjj.qmyx.admin.service.impl;

import com.github.pagehelper.PageHelper;
import com.ytjj.common.api.CommonPage;
import com.ytjj.common.exception.ApiException;
import com.ytjj.qmyx.admin.dao.UserDao;
import com.ytjj.qmyx.admin.mapper.AppListMapper;
import com.ytjj.qmyx.admin.mapper.AppPlatformStrictMapper;
import com.ytjj.qmyx.admin.mapper.PlatformStrictListMapper;
import com.ytjj.qmyx.admin.model.*;
import com.ytjj.qmyx.admin.model.request.AppListRequest;
import com.ytjj.qmyx.admin.model.request.AppPlatformStrictRequest;
import com.ytjj.qmyx.admin.service.AppListManageService;
import com.ytjj.qmyx.users.mapper.UsersInfoMapper;
import com.ytjj.qmyx.users.mapper.UsersMapper;
import com.ytjj.qmyx.users.model.Users;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AppListManageServiceImpl implements AppListManageService {

    @Resource
    private PlatformStrictListMapper platformStrictListMapper;

    @Autowired
    private AppPlatformStrictMapper appPlatformStrictMapper;


    @Resource
    private UsersMapper usersMapper;

    @Override
    public CommonPage selectAppList(AppListRequest request) {
        PlatformStrictListExample example = new PlatformStrictListExample();
        example.setOrderByClause("id desc");
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<PlatformStrictList> list = platformStrictListMapper.selectByExample(example);
        list.forEach(item ->{
            item.setListNum(0);
            AppPlatformStrictExample appPlatformStrictExample = new AppPlatformStrictExample();
            appPlatformStrictExample.createCriteria().andAppListIdEqualTo(item.getId());
            List<AppPlatformStrict> appPlatformStricts = appPlatformStrictMapper.selectByExample(appPlatformStrictExample);
            if (!CollectionUtils.isEmpty(appPlatformStricts)){
                item.setListNum(appPlatformStricts.size());
            }
        });
        CommonPage page = CommonPage.restPage(list);
        return page;
    }

    @Override
    public int createAppList(AppListRequest request) {
        judgeIsExist(request,"create");
        PlatformStrictList platformStrictList = new PlatformStrictList();
        platformStrictList.setListType(request.getListType());
//        platformStrictList.setListNum(request.getListNum());
        platformStrictList.setListSort(request.getListSort());
        platformStrictList.setType(request.getType());
        platformStrictList.setCreateTime(new Date());
        return platformStrictListMapper.insert(platformStrictList);
    }

    @Override
    public int updateAppList(AppListRequest request) {
        judgeIsExist(request,"update");
        PlatformStrictList platformStrictList = new PlatformStrictList();
        platformStrictList.setId(request.getId());
        platformStrictList.setListType(request.getListType());
//        platformStrictList.setListNum(request.getListNum());
        platformStrictList.setListSort(request.getListSort());
        platformStrictList.setType(request.getType());
        return platformStrictListMapper.updateByPrimaryKeySelective(platformStrictList);
    }

    public void judgeIsExist(AppListRequest request,String type) {
        PlatformStrictListExample example = new PlatformStrictListExample();
        example.createCriteria().andListTypeEqualTo(request.getListType());
        List<PlatformStrictList> list = platformStrictListMapper.selectByExample(example);
        if ("create".equals(type)){
            if (!CollectionUtils.isEmpty(list)) {
                throw new ApiException("该榜单类型已存在!");
            }
        }else {
            if (!CollectionUtils.isEmpty(list) && list.get(0).getId().compareTo(request.getId()) != 0) {
                throw new ApiException("该榜单类型已存在!");
            }
        }
    }

    @Override
    public int deleteAppList(Integer id) {
        AppPlatformStrictExample appPlatformStrictExample = new AppPlatformStrictExample();
        appPlatformStrictExample.createCriteria().andAppListIdEqualTo(id);
        List<AppPlatformStrict> appPlatformStricts = appPlatformStrictMapper.selectByExample(appPlatformStrictExample);
        if (!CollectionUtils.isEmpty(appPlatformStricts)){
            throw new ApiException("该榜单下有达人存在,不能删除榜单!");
        }
        return platformStrictListMapper.deleteByPrimaryKey(id);
    }

    @Override
    public CommonPage selectPlatformStrict(AppPlatformStrictRequest request) {
        AppPlatformStrictExample example = new AppPlatformStrictExample();
        if (!StringUtils.isEmpty(request.getAppListId())){
            example.createCriteria().andAppListIdEqualTo(request.getAppListId());
        }
        PageHelper.startPage(request.getPageNum(), request.getPageSize());
        List<AppPlatformStrict> list = appPlatformStrictMapper.selectByExample(example);
        list.forEach(item ->{
            Users users = usersMapper.selectByPrimaryKey(item.getUserId());
            if (null != users && users.getNickname() != null){
                item.setNickName(users.getNickname());
            }
//            PlatformStrictList platformStrictList = platformStrictListMapper.selectByPrimaryKey(request.getAppListId());
//            if (null != platformStrictList && !StringUtils.isEmpty(platformStrictList.getListType())){
//                item.setUserType(platformStrictList.getListType());
//            }
        });
        CommonPage page = CommonPage.restPage(list);
        return page;
    }

    @Override
    public int createPlatformStrict(AppPlatformStrictRequest request) {
        AppPlatformStrictExample example = new AppPlatformStrictExample();
        example.createCriteria().andAppListIdEqualTo(request.getAppListId()).andUserIdEqualTo(request.getUserId());
        List<AppPlatformStrict> list = appPlatformStrictMapper.selectByExample(example);
        if (!CollectionUtils.isEmpty(list)){
            throw new ApiException("该用户已存在该榜单中");
        }
        AppPlatformStrict appPlatformStrict = new AppPlatformStrict();
        appPlatformStrict.setAppListId(request.getAppListId());
        appPlatformStrict.setProductId(request.getProductId());
        appPlatformStrict.setUserId(request.getUserId());
        appPlatformStrict.setUserType(request.getUserType());
        appPlatformStrict.setUserSort(request.getUserSort());
        return appPlatformStrictMapper.insertSelective(appPlatformStrict);
    }

    @Override
    public int updatePlatformStrict(AppPlatformStrictRequest request) {
        if (null == request.getSubsidyMoney()){
            AppPlatformStrictExample example = new AppPlatformStrictExample();
            example.createCriteria().andAppListIdEqualTo(request.getAppListId()).andUserIdEqualTo(request.getUserId());
            List<AppPlatformStrict> list = appPlatformStrictMapper.selectByExample(example);
            if (!CollectionUtils.isEmpty(list) && list.get(0).getUserId().compareTo(request.getUserId()) != 0){
                throw new ApiException("该用户已存在该榜单中");
            }
        }
        AppPlatformStrict appPlatformStrict = new AppPlatformStrict();
        appPlatformStrict.setId(request.getId());
        appPlatformStrict.setAppListId(request.getAppListId());
        appPlatformStrict.setProductId(request.getProductId());
        appPlatformStrict.setUserId(request.getUserId());
        appPlatformStrict.setUserType(request.getUserType());
        appPlatformStrict.setUserSort(request.getUserSort());
        appPlatformStrict.setSubsidyMoney(request.getSubsidyMoney());
        appPlatformStrict.setSubsidySort(request.getSubsidySort());
        return appPlatformStrictMapper.updateByPrimaryKeySelective(appPlatformStrict);
    }

    @Override
    public int deletePlatformStrict(Integer id) {
        return appPlatformStrictMapper.deleteByPrimaryKey(id);
    }

    @Override
    public List<String> getUserType() {
        return appPlatformStrictMapper.getUserType();
    }
}
